<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("This test examines the order of the elements attribute of a form element.");

var container = document.createElement('div');
document.body.appendChild(container);
container.innerHTML = '<input name=victim id=before1 form=owner />' +
    '<input name=victim id=before2 form=owner />' +
    '<form id=owner action= method=GET>' +
    '    <input name=victim id=inner1 form=owner />' +
    '    <input name=victim id=inner2 form=owner />' +
    '</form>' +
    '<input name=victim id=after1 form=owner />' +
    '<input name=victim id=after2 form=owner />';

var owner = document.getElementById('owner');
var before1 = document.getElementById('before1');
var before2 = document.getElementById('before2');
var inner1 = document.getElementById('inner1');
var inner2 = document.getElementById('inner2');
var after1 = document.getElementById('after1');
var after2 = document.getElementById('after2');

debug('- Test for the case where some elements are outside of the form.');
shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for changing the value of the form attribute of a element which is located before the form owner.');
before2.attributes['form'].value = '';
shouldBe('owner.elements.length', '5');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'inner1');
shouldBe('owner.elements[2]', 'inner2');
shouldBe('owner.elements[3]', 'after1');
shouldBe('owner.elements[4]', 'after2');
before2.attributes['form'].value = 'owner';
shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for changing the value of the form attribute of a element which is located inside of the form owner.');
inner2.attributes['form'].value = '';
shouldBe('owner.elements.length', '5');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'after1');
shouldBe('owner.elements[4]', 'after2');
inner2.attributes['form'].value = 'owner';
shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for changing the value of the form attribute of a element which is located after the form owner.');
after1.attributes['form'].value = '';
shouldBe('owner.elements.length', '5');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after2');
after1.attributes['form'].value = 'owner';
shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for setting form attribute of elements in reverse order.');
before1.attributes['form'].value = '';
before2.attributes['form'].value = '';
inner1.attributes['form'].value = '';
inner2.attributes['form'].value = '';
after1.attributes['form'].value = '';
after2.attributes['form'].value = '';

after2.attributes['form'].value = 'owner';
after1.attributes['form'].value = 'owner';
inner2.attributes['form'].value = 'owner';
inner1.attributes['form'].value = 'owner';
before2.attributes['form'].value = 'owner';
before1.attributes['form'].value = 'owner';

shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for setting form attribute of elements in random order.');
before1.attributes['form'].value = '';
before2.attributes['form'].value = '';
inner1.attributes['form'].value = '';
inner2.attributes['form'].value = '';
after1.attributes['form'].value = '';
after2.attributes['form'].value = '';

after1.attributes['form'].value = 'owner';
before1.attributes['form'].value = 'owner';
inner2.attributes['form'].value = 'owner';
before2.attributes['form'].value = 'owner';
after2.attributes['form'].value = 'owner';
inner1.attributes['form'].value = 'owner';

shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');

debug('');
debug('- Test for removing/adding elements');
container.removeChild(before2);
owner.removeChild(inner2);
container.removeChild(after1);
shouldBe('owner.elements.length', '3');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'inner1');
shouldBe('owner.elements[2]', 'after2');
container.insertBefore(before2, owner);
owner.appendChild(inner2);
container.insertBefore(after1, after2);
shouldBe('owner.elements.length', '6');
shouldBe('owner.elements[0]', 'before1');
shouldBe('owner.elements[1]', 'before2');
shouldBe('owner.elements[2]', 'inner1');
shouldBe('owner.elements[3]', 'inner2');
shouldBe('owner.elements[4]', 'after1');
shouldBe('owner.elements[5]', 'after2');
</script>
</body>
</html>
